package com.wenshi.system.init;


import com.wenshi.common.core.domain.ModbusFrame;
import com.wenshi.common.utils.StringUtils;
import com.wenshi.system.domain.ModbusMapping;
import com.wenshi.system.service.ModbusMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

@Component
public  class InitMappingMap {
    public static  Map<String,String> plcAndDescMap = new HashMap<>();
    public static ConcurrentHashMap<Integer,Optional<Integer>> modbusAndResultMap = new ConcurrentHashMap<>();
    public static Map<Integer,String> modbusAndPlcMap = new HashMap<>();
    public static Map<Integer,String> modbusAndDescMap = new HashMap<>();
    public static ConcurrentHashMap<Integer, Optional<String>> modbusAndNameMap = new ConcurrentHashMap<>();
    public static ConcurrentHashMap<String,Integer> nameAndResultMap = new ConcurrentHashMap<>();
    public static Map<String,String> nameAndDescMap = new HashMap<>();
    public static ConcurrentHashMap<String,ConcurrentHashMap<String,Integer>> resultAndDescMap = new ConcurrentHashMap<>();
    public static ConcurrentHashMap<Integer, ModbusFrame> integerModbusFrameMap = new ConcurrentHashMap<>();
    public static Map<String,Map<String,String>> nameAndAlarmCodeMap = new HashMap<>();
    public static Map<Integer, CompletableFuture<Boolean>> pendingRequests = new ConcurrentHashMap<>();

    @Autowired
    private ModbusMappingService modbusMappingService;
    public static Map<String, String> getHeaderAlias() {
        Map<String, String> headerAlias = new HashMap<>();
        headerAlias.put("plc地址", "plcAddress");
        headerAlias.put("modbus 通讯地址", "modbusAddress");
        headerAlias.put("功能码", "functionCode");
        headerAlias.put("设备内容字段", "deviceName");
        headerAlias.put("设备内容描述", "deviceDesc");
        headerAlias.put("注释", "functionDesc");
        headerAlias.put("告警代码", "alarmCode");
        return headerAlias;
    }

    public void  init(){
        List<ModbusMapping> mappingList = modbusMappingService.list();
        for (ModbusMapping modbusMapping : mappingList) {
            String plcAddress = modbusMapping.getPlcAddress();
            String functionDesc = modbusMapping.getFunctionDesc();
            Integer modbusAddress = modbusMapping.getModbusAddress();
            String deviceName = modbusMapping.getDeviceName();
            String deviceDesc = modbusMapping.getDeviceDesc();
            String alarmCode = modbusMapping.getAlarmCode();
            if(modbusAddress==null){
                continue;
            }
            plcAndDescMap.put(plcAddress,functionDesc);
            modbusAndDescMap.put(modbusAddress,functionDesc);
            modbusAndPlcMap.put(modbusAddress, plcAddress);
            modbusAndNameMap.put(modbusAddress,toOptional(deviceName));
            if (deviceName!=null&&deviceName!=" "){
                nameAndDescMap.put(deviceName,deviceDesc);
            }
            if (StringUtils.isNotEmpty(alarmCode)){
               Map<String,String> map = new HashMap<>();
                map.put(deviceDesc, alarmCode);
                nameAndAlarmCodeMap.put(deviceName,map);
            }
        }

    }
    public static Optional<String> toOptional(String value) {
        return (value == null || value.isEmpty()) ? Optional.empty() : Optional.of(value);
    }
}
